forum

Home / DeveloperSection / Forums / easy way to find vowels and print them in JAVA

easy way to find vowels and print them in JAVA

Jayden Bell207705-Nov-2014
Hey this is my first time posting! I got my program to print out the vowels from an input from user but I feel like I have repeated myself a lot in the for loop. Is there a quicker way to do this? Also is this code readable and in the correct format?

import java.util.Scanner;
ublicclassTask09 {
publicstaticvoid main(String[] args)
{
 
   Scanner input = new Scanner(System.in);
   String vowels ="";
   String answer= input.next()
   for(int i = 0 ;i<answer.length();i++)
   {
       char answerPosition = answer.charAt(i);
       if (answerPosition =='a'
           ||answerPosition  =='e'
           ||answerPosition  =='i'
           ||answerPosition =='o'
           ||answerPosition =='u'
           ||answerPosition =='A'
           ||answerPosition =='I'
           ||answerPosition =='O'
           ||answerPosition =='U')
        {
              vowels += answerPosition + " ";
        }
 
    }
 }

import java.util.Scanner;
ublicclassTask09 {
publicstaticvoid main(String[] args)
{
 
   Scanner input = new Scanner(System.in);
   String vowels ="";
   String answer= input.next()
   for(int i = 0 ;i<answer.length();i++)
   {
       char answerPosition = answer.charAt(i);
       if (answerPosition =='a'
           ||answerPosition  =='e'
           ||answerPosition  =='i'
           ||answerPosition =='o'
           ||answerPosition =='u'
           ||answerPosition =='A'
           ||answerPosition =='I'
           ||answerPosition =='O'
           ||answerPosition =='U')
        {
              vowels += answerPosition + " ";
        }
 
    }
System.out.println("The vowels are:" + vowels);
input.close();
 }
}


Updated on 05-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By